﻿<?php
Class User extends CI_Model
{
	function get_user($username)
	{
		$this -> db -> select('user_id, username, password, salt, email, is_admin, created_date, full_name, last_login');
		$this -> db -> from('user');
		$this -> db -> where('username = ' . "'" . $username . "'");
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->row();
		}
		else
		{
			return false;
		}
	}
	
	function update_last_login($user_id, $current_login_time)
	{
		$data = array(
			'last_login' => $current_login_time
		);
		$this -> db -> where('user_id', $user_id);
		$this -> db -> update('user', $data);
	}
	
	function add_user($username, $password, $email, $is_admin, $full_name)
	{
		//calculate hash password and salt
		$max_salt = CRYPT_SALT_LENGTH;

		//blowfish hashing with a salt as follows: "$2a$", a two digit cost parameter, "$", and 22 base 64
		$sha512 = '$6$rounds=5000$';

		$options = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
		//get the longest salt, could set to 22 crypt ignores extra data
		$salt = '';

		for($i = 0; $i <= $max_salt; $i ++) {
			$options = str_shuffle ( $options );
			$salt .= $options [mt_rand ( 0, 63 )];
		}
		
		//get a second salt to strengthen password
		$salt2 = '';

		for($i = 0; $i <= 30; $i ++) {
			$options = str_shuffle ( $options );
			$salt2 .= $options [mt_rand ( 0, 63 )];
		}

		//append salt2 data to the password, and crypt using salt, results in a 60 char output
		$crypt_pass = crypt ( $password . $salt2, $sha512 . $salt );
		
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$data = array (
			'username' => $username,
			'password' => $crypt_pass,
			'salt' => $salt2,
			'email' => $email,
			'is_admin' => $is_admin,
			'created_date' => date("Y/m/d H:i:s", time()),
			'full_name' => $full_name,
			'last_login' => NULL
		);
		$this -> db -> insert('user', $data);
		
		//recheck whether user is inserted successfully
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function delete_user($user_id)
	{
		$this->db->delete('user', array('user_id' => $user_id));
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function update_user($full_name, $email, $user_id)
	{
		$data = array(
			'full_name' => $full_name,
			'email' => $email
		);
		
		$this -> db -> where ('user_id', $user_id);
		$this -> db -> update('user', $data);
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function update_pass($password, $user_id)
	{
		//calculate hash password and salt
		$max_salt = CRYPT_SALT_LENGTH;

		//blowfish hashing with a salt as follows: "$2a$", a two digit cost parameter, "$", and 22 base 64
		$sha512 = '$6$rounds=5000$';

		$options = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
		//get the longest salt, could set to 22 crypt ignores extra data
		$salt = '';

		for($i = 0; $i <= $max_salt; $i ++) {
			$options = str_shuffle ( $options );
			$salt .= $options [mt_rand ( 0, 63 )];
		}
		
		//get a second salt to strengthen password
		$salt2 = '';

		for($i = 0; $i <= 30; $i ++) {
			$options = str_shuffle ( $options );
			$salt2 .= $options [mt_rand ( 0, 63 )];
		}

		//append salt2 data to the password, and crypt using salt, results in a 60 char output
		$crypt_pass = crypt ( $password . $salt2, $sha512 . $salt );
		
		$data = array(
			'password' => $crypt_pass,
			'salt' => $salt2
		);
		
		$this -> db -> where ('user_id', $user_id);
		$this -> db -> update('user', $data);
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function get_total_user()
	{	
		$this->db->select('*');	
		$this->db->from('user');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_today_user()
	{
		$this->db->select('*');	
		$this->db->from('user');
		$this->db->where('date(last_login) = date(now())');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_statistic_user()
	{
		$this->db->select('username, created_date');	
		$this->db->from('user');
		$this->db->limit(5);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
			return $query->result();
		else
			return NULL;
	}
}
?>